#include"programnode.h"
#include"integernode.h"
#include<sstream>

ProgramNode::ProgramNode(VariableNode const & v, ExpressionNode const * e) : name(v), value(e -> copy()), next(0) {}
	
ProgramNode::ProgramNode(ProgramNode const & p) : name(p.name), value((p.value) -> copy()), next(p.next ? new ProgramNode(*p.next) : 0) {}
	
ProgramNode::~ProgramNode() {
	delete value;
	delete next;
}
	
ProgramNode * ProgramNode::append(ProgramNode * p) {
	next = p;
	return this;
}

ProgramNode * ProgramNode::append(VariableNode const & v, ExpressionNode const * e) {
	next = new ProgramNode(v,e);
	return this;
}

ExpressionNode * ProgramNode::expand(VariableNode const & v) const {
	if (name == v)
		return value -> copy();
	else
		return next ? (next -> expand(v)) : (new IntegerNode(0, true));
}

string ProgramNode::toString() const{
	stringstream s;
	s << name.toString() << " := " << (value -> toString()) << endl;
	if (next)
		s << (next -> toString());
	return s.str();
}
